/**
 * 
 */
package com.agilegroup.crm.ui;

import java.util.Date;

import roboguice.inject.InjectView;
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Intent;
import android.net.Uri;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.actionbarsherlock.app.SherlockDialogFragment;
import com.actionbarsherlock.view.MenuItem;
import com.agilegroup.crm.R;
import com.agilegroup.crm.ui.actions.MenuHandler;
import com.agilegroup.crm.ui.fragments.ConnectionDialogFragment;
import com.agilegroup.crm.ui.fragments.ExitDialogFragment;
import com.agilegroup.crm.utils.OnlineChecker;
import com.github.rtyley.android.sherlock.roboguice.activity.RoboSherlockFragmentActivity;

/**
 * @author Hisham
 *
 */
public class MainActivity extends RoboSherlockFragmentActivity {

	@InjectView(R.id.btnLeads)
	private Button btnLeads;
	@InjectView(R.id.btnAccounts)
	private Button btnAccounts;
	@InjectView(R.id.btnContacts)
	private Button btnContacts;
	
	@InjectView(R.id.btnCalls)
	private Button btnCalls;
	@InjectView(R.id.btnAppointments)
	private Button btnAppointments;
	@InjectView(R.id.btnEvents)
	private Button btnEvents;
	
	private OnlineChecker checker;
	
	protected void onCreate(android.os.Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.main_screen);
		 
		 checker = new OnlineChecker(this);
//		 btnLeads = (Button)findViewById(R.id.btnLeads);
//		 btnAccounts = (Button)findViewById(R.id.btnAccounts);
//		 btnContacts = (Button)findViewById(R.id.btnContacts);
		 
//		 btnCalls = (Button)findViewById(R.id.btnCalls);
//		 btnAppointments = (Button)findViewById(R.id.btnAppointments);
		 
//		 btnEvents = (Button)findViewById(R.id.btnEvents);
		 
		 btnLeads.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(checker.isOnline()==true){
					Intent leadActivity = new Intent(MainActivity.this,LeadActivity.class);
					startActivity(leadActivity);
				}
				else{
					SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
				}
			}
		});
		 btnAccounts.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(checker.isOnline()==true){
						Intent accountActivity = new Intent(MainActivity.this,AccountActivity.class);
						startActivity(accountActivity);
					}
					else{
						SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
	        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
					}
				}
			});
		 btnContacts.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(checker.isOnline()==true){
						Intent contactActivity = new Intent(MainActivity.this,ContactActivity.class);
						startActivity(contactActivity);
					}
					else{
						SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
	        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
					}
				}
			});
		 
		 btnCalls.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(checker.isOnline()==true){
						Intent callActivity = new Intent(MainActivity.this,CallActivity.class);
						startActivity(callActivity);
					}
					else{
						SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
	        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
					}
				}
			});
		 
		 btnAppointments.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					if(checker.isOnline()==true){
						Intent appointmentActivity = new Intent(MainActivity.this,AppointmentActivity.class);
						startActivity(appointmentActivity);
					}
					else{
						SherlockDialogFragment dialogFragment = new ConnectionDialogFragment();
	        			dialogFragment.show(getSupportFragmentManager(), "connectionDialog");
					}
				}
			});
		 
		 btnEvents.setOnClickListener(new OnClickListener() {
			
			@SuppressLint("NewApi")
			@Override
			public void onClick(View v) {

				 long startMillis = new Date().getTime();
				 Uri calUri = (Uri.parse("content://com.android.calendar"));
				 Uri.Builder builder = calUri.buildUpon();   
				 builder.appendPath("time");
				 ContentUris.appendId(builder, startMillis);
				 Intent intent = new Intent(Intent.ACTION_VIEW)
				     .setData(builder.build());
				   startActivity(intent);
				   
				
			}
		});
	};
	@Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    	if (keyCode == KeyEvent.KEYCODE_BACK) {
            SherlockDialogFragment newFragment = new ExitDialogFragment();
    	    newFragment.show(getSupportFragmentManager(), "exitDialog");
    	}
    	return super.onKeyDown(keyCode, event);
    }
	
	
	@Override
    public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main_screen, menu);
        return super.onCreateOptionsMenu(menu);
    }
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		MenuHandler.handleOnItemSelected(item, this);
		return super.onOptionsItemSelected(item);
	}
}
